package com.fhz.day19.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fhz.day19.MainActivity2;
import com.fhz.day19.R;
import com.fhz.day19.adapter.MusicAdapter;
import com.fhz.day19.model.Song;
import com.fhz.day19.utils.MusicUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class OnlineFragment extends Fragment {


    private EditText searchTitle;
    private Button search;
    private ImageView onlineImage;
    private TextView onlineTitle;
    private SeekBar onlineSeekBar;
    private List<Song.Data> songList;
    private MusicAdapter musicAdapter;
    private ListView lv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_online, container, false);


        lv = (ListView)inflate.findViewById(R.id.lv);

        searchTitle = (EditText) inflate.findViewById(R.id.search_title);
        search = (Button) inflate.findViewById(R.id.search);
        onlineImage = (ImageView) inflate.findViewById(R.id.online_image);
        onlineTitle = (TextView) inflate.findViewById(R.id.online_title);
        onlineSeekBar = (SeekBar) inflate.findViewById(R.id.online_seekBar);


        //调往带歌词的界面
        onlineImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MainActivity2.class));
            }
        });

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = searchTitle.getText().toString();
                //开始去网络搜索
                //异步下载..
                new MyTask().execute("http://39.106.204.190:7777/music/search?singer=".concat(title));
            }
        });

        //创建适配器
       songList = new ArrayList<>();
       musicAdapter = new MusicAdapter(songList, getActivity());
       lv.setAdapter(musicAdapter);



       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               MusicUtils.getInstance().play(position); //邓紫棋

               onlineTitle.setText(MusicUtils.getInstance().getTitle());
               Glide.with(getActivity()).load(MusicUtils.getInstance().getImageUrl()).into(onlineImage);

           }
       });

       //定时器
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                onlineSeekBar.setMax(MusicUtils.getInstance().getMax());
                onlineSeekBar.setProgress(MusicUtils.getInstance().getCurrentPostion());
            }
        },0,1000);


        onlineSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
               if(fromUser){
                   MusicUtils.getInstance().seekTo(progress);
               }
           }

           @Override
           public void onStartTrackingTouch(SeekBar seekBar) {

           }

           @Override
           public void onStopTrackingTouch(SeekBar seekBar) {

           }
       });

        return inflate;

    }

    class MyTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            StringBuffer sb = new StringBuffer();
            HttpURLConnection connection = null;
            InputStream inputStream = null;
            try {
                URL url=new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode()==200) {
                    inputStream = connection.getInputStream();
                    byte[] b = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(b)) != -1) {
                        sb.append(new String(b, 0, len));
                    }
                    String s = sb.toString();
                    return s;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    connection.disconnect();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //对下载完成的字符,进行解析.生成对应的实体类.
            Song song = new Gson().fromJson(s, Song.class);
            //更新我们的适配器内容了.
            List<Song.Data> data = song.getData();

            //记得给工具类设置数据源.否则就空指针了
            MusicUtils.getInstance().setDataList(data);

            //邓紫棋
            //更新数据,刷新适配器的内容
            songList.clear();
            songList.addAll(data);
            musicAdapter.notifyDataSetChanged();
        }
    }


}